/**
 * @author        Nicolas Bottarini <nicolasbottarini@gmail.com>, 404 <http://www.proyecto404.com>
 * @version       $Rev: 3 $
 * @lastrevision  $Date: 2011-09-12 16:45:37 +0000 (Mon, 12 Sep 2011) $	 
 */
package com.proyecto404.core.mvc {
	import com.proyecto404.core.BaseObject;
	import com.proyecto404.core.Check;
	import com.proyecto404.core.EventArgs;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.filters.BitmapFilter;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.BlurFilter;
	
	public class DefaultPopupManager extends PopupManager {
		private const _overlayAlpha:Number = 0.16;
		private const _modalBackgroundBlur:int = 4;
		
		private var _overlay:MovieClip;
		private var _containerFilters:Array;
		
		public function DefaultPopupManager(contentLayer:DisplayObjectContainer, popupLayer:DisplayObjectContainer) {
			super(contentLayer, popupLayer);
			
			createOverlay();
			contentLayer.stage.addEventListener(Event.RESIZE, onResize);
			contentLayer.stage.addEventListener(Event.FULLSCREEN, onResize);
		}
		
		public override function showPopup(popupView:View, onClose:Function = null):void {
			Check.require(_currentPopup == null, "There is already an opened popup");
			
			popupView.closed.addOnce(function(e:EventArgs):void {
				doClosePopup(onClose);
			});
			_currentPopup = popupView;
			_containerFilters = contentLayer.filters.concat(); // create copy
			var newFilters:Array = contentLayer.filters.concat(); // create copy
			newFilters.push(getBlurFilter());
			contentLayer.filters = newFilters;
			contentLayer.addChild(_overlay);
			popupLayer.addChild(_currentPopup);
			onResize();
			_currentPopup.open();
		}
		
		private function doClosePopup(onClose:Function = null):void {
			popupLayer.removeChild(_currentPopup);
			contentLayer.removeChild(_overlay);
			contentLayer.filters = _containerFilters;
			_currentPopup = null;
			if (onClose != null) {
				onClose();
			}
		}
		
		public override function closePopup(onClose:Function = null):void {
			Check.require(_currentPopup != null, "There is no opened popup");
			if (!_currentPopup.isClosed()) {
				if (onClose != null) {
					_currentPopup.closed.add(function(e:EventArgs):void {
						onClose();
					});
				}
				_currentPopup.close();
			}
		}
		
		private function createOverlay():void {
			_overlay = new MovieClip();
			var g:Graphics = _overlay.graphics as Graphics;
			g.beginFill(0xeeeeee, _overlayAlpha);
			g.moveTo(0,0);
			g.lineTo(100, 0);
			g.lineTo(100, 100);
			g.lineTo(0, 100);
			g.lineTo(0, 0);
			g.endFill();
			onResize();
		}
		
		private function onResize(e:Event = null):void {
			_overlay.width = contentLayer.stage.stageWidth;
			_overlay.height = contentLayer.stage.stageHeight;
			// TODO: Ver de centrarlo bien y los problemas de resize del stage
			if (_currentPopup != null ) {
				_currentPopup.x = (contentLayer.stage.stageWidth - _currentPopup.width)/2;
				_currentPopup.y = (contentLayer.stage.stageHeight - _currentPopup.height)/2;
			}
		}
		
		private function getBlurFilter():BitmapFilter {
			var blurFilter:BlurFilter = new BlurFilter();
			blurFilter.blurX = _modalBackgroundBlur;
			blurFilter.blurY = _modalBackgroundBlur;
			blurFilter.quality = BitmapFilterQuality.HIGH;
			return blurFilter;
		}
	}
}